using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;

namespace tKitchen
{
    public class MainPresenter
    {
        tKitchenMain domain;
        Main mainForm;
        List<Recipe> recipeList;

        #region Properties
        public Main Form
        {
            get { return mainForm; }
            set { mainForm = value; }
        }
        #endregion

        public MainPresenter(tKitchenMain domainIn)
        {
            domain = domainIn;
            domain.RecipeUpdated += new EventHandler(domain_RecipeUpdated);
            domain.RecipeAdded += new EventHandler(domain_RecipeAdded);

            domain.MealPlanAdded += new EventHandler(domain_MealPlanAdded);
            domain.MealPlanUpdated += new EventHandler(domain_MealPlanUpdated);


            mainForm = new Main(this);

            //Recipe Tab////////////
            mainForm.IngredientButtonClick += new EventHandler(mainForm_IngredientButtonClick);
            mainForm.UnitButtonClick += new EventHandler(mainForm_UnitButtonClick);
            mainForm.CategoryButtonClick += new EventHandler(mainForm_CategoryButtonClick);
            mainForm.RecipeButtonClick += new EventHandler(mainForm_RecipeButtonClick);
            mainForm.DBResetButtonClick += new EventHandler(mainForm_DBResetButtonClick);
            mainForm.RecipeDoubleClick += new EventHandler(mainForm_RecipeDoubleClick);
            mainForm.DeleteButtonClick += new EventHandler(mainForm_DeleteButtonClick);

            recipeList = domain.GetRecipeList();
            PopulateRecipeGridView();



            //Meal Plan Tab///////////
            mainForm.MealButtonClick += new EventHandler(mainForm_MealButtonClick);
            mainForm.FowardButtonClick += new EventHandler(mainForm_FowardButtonClick);
            mainForm.BackButtonClick += new EventHandler(mainForm_BackButtonClick);
            mainForm.MealDoubleClicked += new EventHandler(mainForm_MealDoubleClicked);
            mainForm.NewMealMenuItemClicked += new EventHandler(mainForm_NewMealMenuItemClicked);
            mainForm.DeleteMealMenuItemClicked += new EventHandler(mainForm_DeleteMealMenuItemClicked);
            mainForm.ShoppingListButtonClicked += new EventHandler(mainForm_ShoppingListButtonClicked);
            mainForm.StartDateChanged += new EventHandler(mainForm_StartDateChanged);
            mainForm.EndDateChanged += new EventHandler(mainForm_EndDateChanged);


            mainForm.DateLabel = mainForm.Calendar.MonthYear;
            mainForm.Calendar.SetDatasource(domain.GetMealPlanList());
            mainForm.Calendar.DisplayCalendar();
        }

        private void PopulateRecipeGridView()
        {
            mainForm.Recipes.DataSource = null;
            mainForm.Recipes.DataSource = recipeList;
            mainForm.Recipes.Columns["Ingredients"].Visible = false;
            mainForm.Recipes.Columns["Comments"].Visible = false;
            mainForm.Recipes.Columns["Steps"].Visible = false;
            mainForm.Recipes.Columns["RecipeId"].Visible = false;
            mainForm.Recipes.Columns["Rating"].Visible = false;
        }


        #region Meal Plan Tab Event Handlers
        void mainForm_ShoppingListButtonClicked(object sender, EventArgs e)
        {
            //get the start and end dates
            DateTime start = mainForm.StartDate;
            DateTime end = mainForm.EndDate;

            //get the list of ingredients
            //and create a new shopping list presenter
            List<Recipe_Ingredient> ingList = domain.GetShoppingList(start, end);
            ShoppingListPresenter presenter = new ShoppingListPresenter(domain, ingList);

            //show the form
            presenter.ShowForm();
        }
        void mainForm_StartDateChanged(object sender, EventArgs e)
        {
            //if the end date is before the new start date
            //change the end date to the start date
            DateTime start = mainForm.StartDate;
            DateTime end = mainForm.EndDate;
            if (end.CompareTo(start) < 0)
            {
                mainForm.EndDate = start;
            }
        }

        void mainForm_EndDateChanged(object sender, EventArgs e)
        {
            //if the start date is after the new end date
            //change the start date to the end date
            DateTime start = mainForm.StartDate;
            DateTime end = mainForm.EndDate;
            if (start.CompareTo(end) > 0)
            {
                mainForm.StartDate = end;
            }
        }

        void mainForm_DeleteMealMenuItemClicked(object sender, EventArgs e)
        {
            ListBox temp = (ListBox)sender;
            MealPlan tempMealPlan = (MealPlan)temp.SelectedItem;
            domain.DeleteObject(tempMealPlan);
            mainForm.Calendar.SetDatasource(domain.GetMealPlanList());
            mainForm.Calendar.DisplayCalendar();

        }

        void mainForm_NewMealMenuItemClicked(object sender, EventArgs e)
        {
            DateTime clickedDate = (DateTime)sender;
            MealPresenter presenter = new MealPresenter(domain, clickedDate);
            presenter.ShowForm();

        }

        void mainForm_MealDoubleClicked(object sender, EventArgs e)
        {
            ListBox temp = (ListBox)sender;
            MealPresenter presenter = new MealPresenter(domain, (MealPlan)temp.SelectedItem);
            presenter.ShowForm();
        }


        void domain_MealPlanUpdated(object sender, EventArgs e)
        {
            mainForm.Calendar.SetDatasource(domain.GetMealPlanList());
            mainForm.Calendar.DisplayCalendar();
        }
        void domain_MealPlanAdded(object sender, EventArgs e)
        {
            mainForm.Calendar.SetDatasource(domain.GetMealPlanList());
            mainForm.Calendar.DisplayCalendar();
        }
        void mainForm_FowardButtonClick(object sender, EventArgs e)
        {
            mainForm.Calendar.Date = mainForm.Calendar.Date.AddMonths(1);
            mainForm.DateLabel = mainForm.Calendar.MonthYear;
        }

        void mainForm_BackButtonClick(object sender, EventArgs e)
        {
            mainForm.Calendar.Date = mainForm.Calendar.Date.AddMonths(-1);
            mainForm.DateLabel = mainForm.Calendar.MonthYear;
        }


        void mainForm_MealButtonClick(object sender, EventArgs e)
        {
            //DateTime currDate = mainForm.Calendar.Date;
            //MealPresenter presenter = new MealPresenter(domain,currDate);
            MealPresenter presenter = new MealPresenter(domain);
            presenter.ShowForm();

        }
        #endregion

        #region Recipe Tab Event Handlers
        void mainForm_DeleteButtonClick(object sender, EventArgs e)
        {
            //get the list of recipes in the datagrid
            List<Recipe> tempList = (List<Recipe>)mainForm.Recipes.DataSource;
            
            //get the id of the selected recipe
            int selectedId = mainForm.Recipes.SelectedRows[0].Cells[0].RowIndex;
            
            //get the selected recipe
            Recipe selectedRecipe = tempList[selectedId];
            
            //remove the recipe from the list and the DB
            recipeList.Remove(selectedRecipe);
            domain.DeleteObject(selectedRecipe);


            //repopulate the display
            PopulateRecipeGridView();
        }
        void mainForm_DBResetButtonClick(object sender, EventArgs e)
        {
            domain.ResetDatabase();
        }

        void mainForm_CategoryButtonClick(object sender, EventArgs e)
        {
            RecipeCategoryPresenter presenter = new RecipeCategoryPresenter(domain);
            presenter.ShowForm();
        }

        void mainForm_UnitButtonClick(object sender, EventArgs e)
        {
            UnitPresenter presenter = new UnitPresenter(domain);
            presenter.ShowForm();
        }

        void mainForm_RecipeDoubleClick(object sender, EventArgs e)
        {

            int index = ((DataGridViewCellEventArgs)e).RowIndex;
            List<Recipe> recipeList = (List<Recipe>)mainForm.Recipes.DataSource;
            Recipe myRecipe = recipeList[index];

            RecipePresenter presenter = new RecipePresenter(domain, myRecipe);
            presenter.ShowForm();

        }

        void mainForm_RecipeButtonClick(object sender, EventArgs e)
        {
            RecipePresenter presenter = new RecipePresenter(domain);
            presenter.ShowForm();
        }

        void domain_RecipeAdded(object sender, EventArgs e)
        {
            //todo: to improve performance, have the event send the added recipe
            //and then add just the recipe in the list
            recipeList = domain.GetRecipeList();
            PopulateRecipeGridView();
        }
        void domain_RecipeUpdated(object sender, EventArgs e)
        {
            //todo: to improve performance, have the event send the updated recipe
            //and then update just the recipe in the list
            recipeList = domain.GetRecipeList();
            PopulateRecipeGridView();
        }



        void mainForm_IngredientButtonClick(object sender, EventArgs e)
        {
            IngredientPresenter presenter = new IngredientPresenter(domain);
            presenter.ShowForm();
        }

        #endregion
        public void ShowForm()
        {
            mainForm.Show();
        }



        #region going to be depricated functions

        internal void ShowIngredientScreen()
        {
            IngredientPresenter ingPresent = new IngredientPresenter(domain);
        }

        internal void ShowUnitScreen()
        {
            UnitPresenter unitPresent = new UnitPresenter(domain);
        }

        internal void ShowRecipeCategoryScreen()
        {
            RecipeCategoryPresenter catPresent = new RecipeCategoryPresenter(domain);
        }
        internal void ShowRecipeScreen()
        {
            RecipePresenter recipePresent = new RecipePresenter(domain);

        }
        internal void ShowRecipeScreen(int index)
        {
            List<Recipe> recipeList = (List<Recipe>)mainForm.Recipes.DataSource;
            Recipe myRecipe = recipeList[index];
            RecipePresenter presenter = new RecipePresenter(domain, myRecipe);

            mainForm.Recipes.Refresh();
        }

        internal void ResetDatabase()
        {
            domain.ResetDatabase();
        }

        #endregion
    }
}
